﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JzSport.WebApp.AdvertisingHTML
{
    public partial class JoinAdvertising : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();

            if (!IsPostBack)
            {
                Guid promotionalActivityId = Guid.Empty;
                if (!Guid.TryParse(Request.QueryString["id"], out promotionalActivityId))
                {
                    Response.Redirect("./Error.aspx");
                }

                int currentUserId = Api.ApiPageUtil.GetCurrentUserId();

                ServiceResult<PromotionalActivity> srPActivity = new PromotionalActivityService().Get(promotionalActivityId);
                if (srPActivity.ErrorCode == ErrorCode.NoError && srPActivity.Data != null)
                {
                    ServiceResult<ActivityDetail> srActivity = new ActivityService().GetActivity(srPActivity.Data.ActivityId);
                    if (srActivity.ErrorCode == ErrorCode.NoError && srActivity.Data != null)
                    {
                        _ctlHiddenActivityId.Value = srPActivity.Data.ActivityId.ToString();
                        _ctlHiddenCurrentUserId.Value = currentUserId.ToString();

                        if (!string.IsNullOrEmpty(srPActivity.Data.PreviewImageUrl))
                        {
                            _ctlImage.Src = string.Format("{0}{1}", Configs.BaseWebUrl, srPActivity.Data.PreviewImageUrl);
                        }

                        _ctlTextTitle.InnerText = srPActivity.Data.Title;
                        _ctlTextAddress.InnerText = srActivity.Data.Address;
                        _ctlTextSponsor.InnerText = srPActivity.Data.Sponsor;
                        _ctlTextBeginTime.InnerText = srActivity.Data.BeginTime.ToString("yyyy-MM-dd hh:mm");
                        _ctlTextEndTime.InnerText = srActivity.Data.EndTime.ToString("yyyy-MM-dd hh:mm");
                        _ctlTextDescription.InnerText = srActivity.Data.Descritpion;
                        _ctlTextCount.InnerText = srActivity.Data.CurrentPersonCount.ToString();

                        if(srPActivity.Data.BeginTime <= DateTime.Now)
                        {
                            _ctlBtnJoin.Visible = false;
                            _ctlBtnAudit.Visible = false;
                            _ctlTextMessage.InnerText = "活动已经开始,正在进行中!";
                            _ctlTextMessage.Visible = true;
                        }
                        else
                        {
                            // 验证用户的身份
                            if (currentUserId == 0)
                            {
                                // 没有获取到帐号
                                _ctlTextMessage.Visible = true;
                            }
                            else
                            {
                                if (new ActivityService().ValidateMember(srActivity.Data.Id, currentUserId, BasicData.ActivityMemberType.Administrator)
                                    || new ActivityService().ValidateMember(srActivity.Data.Id, currentUserId, BasicData.ActivityMemberType.Creator))
                                {
                                    // 是管理员
                                    _ctlBtnAudit.Visible = true;
                                }
                                else if (!new ActivityService().ValidateMember(srActivity.Data.Id, currentUserId, BasicData.ActivityMemberType.Member))
                                {
                                    _ctlBtnJoin.Visible = true;
                                }

                                if (new ActivityService().ValidateHasJoin(srActivity.Data.Id, currentUserId))
                                {
                                    _ctlBtnJoin.Visible = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        Response.Redirect("./Error.aspx");
                    }
                }
                else
                {
                    Response.Redirect("./Error.aspx");
                }
            }
        }

        protected void _ctlBtnJoin_Click(object sender, EventArgs e)
        {
            int currentUserId = Convert.ToInt32(_ctlHiddenCurrentUserId.Value);
            Guid activityId = new Guid(_ctlHiddenActivityId.Value);
            if (new ActivityService().Join(currentUserId, activityId).ErrorCode == ErrorCode.NoError)
            {
                Response.Write(string.Format("<script>alert('申请成功!'); window.location.href='{0}';</script>", Request.RawUrl));
            }
            else
            {
                Response.Write("<script>alert('申请失败!');</script>");
            }
        }

        protected void _ctlBtnAudit_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("./AuditingList.aspx?id={0}&sys:token={1}", Request.QueryString["id"], Request.QueryString["sys:token"]));
        }
    }
}